/***********************************************************************************
# File Name:    sensor_data.c
# Author:       ciyeer
# Mail:         ciyeer@outlook.com 
# Created Time: 2025年11月10日 星期一 18时56分13秒
 **********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <termios.h>
#include <time.h>

#define JSON_RESPONSE "Content-Type: application/json;charset=UTF-8\r\n\r\n"
#define SERIAL_PORT "/dev/ttyUSB0"
#define LOCK_FILE "/tmp/zigbee_lock"

int init_serial();
int acquire_lock();
int release_lock();
int read_sensor_data(char *response, int max_len);

int main() {
    printf(JSON_RESPONSE);
    
    // 读取传感器数据
    char response[256];
    char cmd[128] = "{\"device_type\":\"sensor\",\"action\":\"read\"}";
    
    if (read_sensor_data(cmd, response, sizeof(response)) == 0 && strlen(response) > 0) {
        // 直接返回单片机发送的JSON数据
        printf("%s", response);
    } else {
        // 返回模拟数据
        time_t ts = time(NULL);
        printf("{\"status\":\"success\",\"temperature\":24.5,\"humidity\":55.5,\"timestamp\":%ld}", ts);
    }
    
    return 0;
}

int init_serial() {
    int fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0) return -1;
    
    struct termios tty;
    if (tcgetattr(fd, &tty) != 0) {
        close(fd);
        return -1;
    }
    
    cfsetospeed(&tty, B9600);
    cfsetispeed(&tty, B9600);
    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
    tty.c_iflag &= ~IGNBRK;
    tty.c_lflag = 0;
    tty.c_oflag = 0;
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 5;
    tty.c_iflag &= ~(IXON | IXOFF | IXANY);
    tty.c_cflag |= (CLOCAL | CREAD);
    tty.c_cflag &= ~(PARENB | PARODD);
    tty.c_cflag &= ~CSTOPB;
    tty.c_cflag &= ~CRTSCTS;
    
    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        close(fd);
        return -1;
    }
    
    return fd;
}

int acquire_lock() {
    int lock_fd = open(LOCK_FILE, O_CREAT | O_RDWR, 0666);
    if (lock_fd < 0) return -1;
    if (flock(lock_fd, LOCK_EX | LOCK_NB) != 0) {
        close(lock_fd);
        return -1;
    }
    return lock_fd;
}

int release_lock() {
    int lock_fd = open(LOCK_FILE, O_RDWR);
    if (lock_fd < 0) return -1;
    flock(lock_fd, LOCK_UN);
    close(lock_fd);
    return 0;
}

int read_sensor_data(char *cmd, char *response, int max_len) {
    int lock_fd = acquire_lock();
    if (lock_fd < 0) return -1;
    
    int serial_fd = init_serial();
    if (serial_fd < 0) {
        release_lock();
        return -1;
    }
    
    // 发送读取命令
    char full_cmd[256];
    snprintf(full_cmd, sizeof(full_cmd), "%s\n", cmd);
    write(serial_fd, full_cmd, strlen(full_cmd));
    tcdrain(serial_fd);
    
    // 等待并读取响应
    usleep(500000); // 等待500ms
    int n = read(serial_fd, response, max_len - 1);
    if (n > 0) {
        response[n] = '\0';
        // 去除可能的换行符
        if (n > 0 && response[n-1] == '\n') {
            response[n-1] = '\0';
        }
    } else {
        response[0] = '\0';
        close(serial_fd);
        release_lock();
        return -1;
    }
    
    close(serial_fd);
    release_lock();
    return 0;
}
